package net.xo68.destiny.starter.common.context;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * 本地化消息
 */
@Slf4j
@Component
public class LocaleMessage {

    private final MessageSource messageSource;

    public LocaleMessage(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public String getMessage(String code){
        return getMessage(code,null);
    }
    public String getMessage(String code,@Nullable Object[] args){
        return getMessage(code,args,getDefaultLocale());
    }

    public String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException{
        return this.messageSource.getMessage(code,args,locale);
    }
    public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
        return this.messageSource.getMessage(code,args,defaultMessage,locale);
    }

    private Locale getDefaultLocale(){
        return Locale.SIMPLIFIED_CHINESE;
    }
}
